;================================================================
; Part 2 function in CLIPS
; 2013 Feb 17
; Team 0
;
;================================================================
; 			Revision history			|
;================================================================
; Date		|	Remarks		|	Author		|
;===============+=======================+========================
; 2013 Feb 10	|Part 2 get session	|	Eliz		|
;===============+=======================+========================
; 2013 Feb 17	|Part 2 matching	|	Sam		|
;===============+=======================+========================
; 2013 Mar 12	|Integration P1 & P2	|	Sam		|
;===============+=======================+========================
; 		|			|			|
;===============+=======================+========================
;
;

; sort/integration code + Par2 code starts here...

; Bleow sort_course_list is the interface between sort/integration and part 1 functions
; for sorting Weight (1-28) based on CF
; hightest CF has highest weight (28)
(deftemplate sort_course_list 
	(slot CID (type SYMBOL) (default EMPTY)) 
	(slot NAME (type STRING) (default "EMPTY")) 
	(slot CF (type NUMBER) (default 0.5)) 
	(slot WEIGHT (type NUMBER) (default 0)) 
	(slot SID (type SYMBOL) (default S0))
)
;
;
; Bleow course_list is the interface between sort/integration and part 2 functions
; deftemplate course_list (slot CID) (slot NAME) (slot CF) (slot WEIGHT) (slot SID)
; example: (course_list KE4204 "Business Analytics" 0.5 28 S0)
;
(deftemplate course_list 
	(slot CID (type SYMBOL) (default EMPTY)) 
	(slot NAME (type STRING) (default "EMPTY")) 
	(slot CF (type NUMBER) (default 0.5)) 
	(slot WEIGHT (type NUMBER) (default 0)) 
	(slot SID (type SYMBOL) (default S0))
)

; Below session_list will be used to store the goal.
(deftemplate session_list 
	(slot CID (type SYMBOL) (default EMPTY)) 
	(slot NAME (type STRING) (default "EMPTY")) 
	(slot CF (type NUMBER) (default 0.5))
	(slot WEIGHT (type NUMBER) (default 0)) 
	(slot SID (type SYMBOL) (default S0))
)


; Below "interface_facts" should come from Part 1 function; We hard-coded here for testing purpose
;(deffacts interface_facts
;	(course_list (CID KE4204) (NAME "Business Analytics") (WEIGHT 28))
;	(course_list (CID KE5205) (NAME "Text Mining") (WEIGHT 27))
;	(course_list (CID KE5204) (NAME "Genetic Algorithms") (WEIGHT 26))
;	(course_list (CID SG5220) (NAME "Research on Advanced IT Topics I") (WEIGHT 25))
;	(course_list (CID SG5205) (NAME "Software Metrics & Process Improvement") (WEIGHT 24))
;	(course_list (CID SG4205) (NAME "Information System Security") (WEIGHT 23))
;	(course_list (CID SG4210) (NAME "Enterprise .NET I") (WEIGHT 22))
;	(course_list (CID SG5214) (NAME "Software Requirement Engineering") (WEIGHT 21))
;	(course_list (CID KE4202) (NAME "Case Based Reasoning") (WEIGHT 20))
;	(course_list (CID KE4205) (NAME "Knowledge management") (WEIGHT 19))
;	(course_list (CID SG5221) (NAME "Research on Advanced IT Topics II") (WEIGHT 18))
;	(course_list (CID SG5228) (NAME "Service Innovation") (WEIGHT 17))
;	(course_list (CID SG5219) (NAME "IT Law") (WEIGHT 16))
;	(course_list (CID SG5207) (NAME "Managing IT Outsourcing & Subcontracting") (WEIGHT 15))
;	(course_list (CID SG5211) (NAME "Business Process Management") (WEIGHT 14))
;	(course_list (CID SG5222) (NAME "IT Service management") (WEIGHT 13))
;	(course_list (CID SG5227) (NAME "Open Source for the Enterprise") (WEIGHT 12))
;	(course_list (CID SG4211) (NAME "Cloud Computing") (WEIGHT 11))
;	(course_list (CID SG5231) (NAME "Agile Software Project management") (WEIGHT 10))
;	(course_list (CID SG5226) (NAME "Enterprise .NET II") (WEIGHT 9))
;	(course_list (CID SG5209) (NAME "Enterprise Java") (WEIGHT 8))
;	(course_list (CID SG4206) (NAME "Enterprise Integration") (WEIGHT 7))
;	(course_list (CID SG4202) (NAME "Mobile Wireless Application Development") (WEIGHT 6))
;	(course_list (CID SG3204) (NAME "Human Computer Interface") (WEIGHT 5))
;	(course_list (CID SG5208) (NAME "Object Oriented Design Patterns") (WEIGHT 4))
;	(course_list (CID SG5225) (NAME "Architecting Software Solutions") (WEIGHT 3))
;	(course_list (CID SG5229) (NAME "Software Maintenance & Evolution") (WEIGHT 2))
;	(course_list (CID SG5230) (NAME "Software Prototyping") (WEIGHT 1))
;)

; Below is the look up (reference) table of 8 sessions with available courses (CID)
(deffacts sessions
	(session S1 SG5230 SG4210 SG5205 SG4202 KE5204 SG5219 SG4205)
	(session S2 SG5222 SG5226 SG4211 KE4202 SG5220 SG5214 SG5227)
	(session S3 SG5208 KE5205 SG5228 KE4204 SG5221 SG5225 SG5231)
	(session S4 SG5209 SG5211 SG4206 SG3204 SG5207 KE4205 SG5229)

	(session S5 SG5230 SG4210 SG5205 SG4202 KE5204 SG5219 SG4205)
	(session S6 SG5222 SG5226 SG4211 KE4202 SG5220 SG5214 SG5227)
	(session S7 SG5208 KE5205 SG5228 KE4204 SG5221 SG5225 SG5231)
	(session S8 SG5209 SG5211 SG4206 SG3204 SG5207 KE4205 SG5229)
)

; Below session_list is to be defined as goal, having 8 session to be filled with course CID.
(deffacts session_list
	(session_list (SID S1))
	(session_list (SID S2))
	(session_list (SID S3))
	(session_list (SID S4))
	(session_list (SID S5))
	(session_list (SID S6))
	(session_list (SID S7))
	(session_list (SID S8))
)


; some global variables used to control matching: weighting value from 28 to 1
(defglobal
	?*counter-weight* = 0
)




; Blew is the sort/integration for part 1 and part 2 function
; 
(defrule give_default_weight
 (declare (salience -200))
	(currentGoal (goal ?NAME) (cf ?CF))
=> 
	(assert (sort_course_list (NAME ?NAME) (CF ?CF) (WEIGHT (+ 1 ?*counter-weight*)) )
	)
	(bind ?*counter-weight* (+ 1 ?*counter-weight*))
)

(defrule sort_course_list
 (declare (salience -210))
	?f <- (sort_course_list (CID ?CID1) (NAME ?NAME1) (CF ?CF1) (WEIGHT ?WEIGHT1) (SID ?SID1))
	?g <- (sort_course_list (CID ?CID2) (NAME ?NAME2) (CF ?CF2) (WEIGHT ?WEIGHT2) (SID ?SID2))
		(test (> ?CF1 ?CF2))
    		(test (= (+ ?WEIGHT1 1) ?WEIGHT2))
=>
    	(retract ?f ?g)
	(assert (sort_course_list (CID ?CID1) (NAME ?NAME1) (CF ?CF1) (WEIGHT ?WEIGHT2) (SID ?SID1))
        	(sort_course_list (CID ?CID2) (NAME ?NAME2) (CF ?CF2) (WEIGHT ?WEIGHT1) (SID ?SID2))
	)
)


(defrule format_course_list ; To populate CID
 (declare (salience -220))
	(currentElective (code ?CID) (elective ?NAME))
	(sort_course_list (CID EMPTY) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
=> 
	(assert (course_list (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID) )
	)
)



; Blew is the 1st part of part 2 function: To find available sessions for each Course (CID)
; Update course_list with available sessions (SID)
(defrule get-session
 (declare (salience -400))
	(session ?SID $?first ?CID $?last)
	(course_list (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID1&:(neq ?SID1 ?SID))
	)
=> 
	(assert (course_list (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID) )
	)
;	?f1 <- (course_list ?CID ?NAME ?CF ?WEIGHT ?SID1)
;	(retract ?f1)
;	(printout t "course id is " ?cid crlf)
;	(printout t ?SID crlf) 
)

; Blew is the 2nd part of part 2 function: To match and fill in unique Course (CID) into each sessions
; from high weighting value to low weighting value
; Update session_list with unique course (CID)
;
;=====================================================================
; 2013 Feb 17
; Sam:
; Below method is very foolish, as I couldn't find a way to do loop...
; Pls see if there is more efficient way.
;=====================================================================
;
;
(defrule match28
 (declare (salience -501))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
;	?f2 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT ?*counter-weight*)) (SID ?SID1&:(eq ?SID1 ?SID)))
	?f28 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 28)) (SID ?SID1&:(eq ?SID1 ?SID)))
;	(test (neq ?f1 ?f2)) ; test pointers and not value
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
;	(retract $?f2)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
;	(bind ?*counter-weight* (- ?*counter-weight* 1))
)
(defrule match27
 (declare (salience -502))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f27 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 27)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match26
 (declare (salience -503))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f26 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 26)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match25
 (declare (salience -504))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f25 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 25)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match24
 (declare (salience -505))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f24 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 24)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match23
 (declare (salience -506))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f23 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 23)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match22
 (declare (salience -507))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f22 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 22)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match21
 (declare (salience -508))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f21 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 21)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match20
 (declare (salience -509))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f20 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 20)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match19
 (declare (salience -510))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f19 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 19)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match18
 (declare (salience -511))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f18 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 18)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match17
 (declare (salience -512))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f17 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 17)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match16
 (declare (salience -513))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f16 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 16)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match15
 (declare (salience -514))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f15 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 15)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match14
 (declare (salience -515))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f14 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 14)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match13
 (declare (salience -516))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f13 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 13)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match12
 (declare (salience -517))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f12 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 12)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match11
 (declare (salience -518))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f11 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 11)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match10
 (declare (salience -519))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f10 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 10)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match9
 (declare (salience -520))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f9 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 9)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match8
 (declare (salience -521))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f8 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 8)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match7
 (declare (salience -522))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f7 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 7)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match6
 (declare (salience -523))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f6 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 6)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match5
 (declare (salience -524))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f5 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 5)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match4
 (declare (salience -525))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f4 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 4)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match3
 (declare (salience -526))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f3 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 3)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match2
 (declare (salience -527))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f2 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 2)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)
(defrule match1
 (declare (salience -528))
	?f0 <- (session_list (CID EMPTY) (SID ?SID))
	?f1 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT&:(= ?WEIGHT 1)) (SID ?SID1&:(eq ?SID1 ?SID)))
=>
;	(printout t crlf "course selected&retracted: " ?CID ?NAME ?WEIGHT ?SID crlf)
	(modify ?f0 (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID)
	)
)


; Blew is rule for house keeping: when a course is filled in a session, ALL course_list facts with this couse should be removed/retracted to avoid double matching.
; put salience 

(defrule housekeep
 (declare (salience -300))
	?f1 <- (session_list (CID ?CID))
	?f2 <- (course_list  (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID))
	(test (neq ?f1 ?f2)) ; test pointers and not value
=>
;	(printout t crlf "course retracted         : " ?CID ?NAME ?WEIGHT ?SID crlf)
	(retract $?f2)
)



; Blew is the 3rd part of part 2 function: To print result
; 
;(defrule display_result
; (declare (salience -100))
;	(session_list (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID ?SID))
;=>
;	(printout t crlf "| Session: " ?SID " - " ?CID " - " ?NAME " - " ?WEIGHT " - " ?CF " " crlf)
;)

(defrule display_result1
 (declare (salience -701))
	(session_list (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID S1))
=>
	(printout t crlf "| ---------------------------------------------------------------------------")
	(printout t crlf "| Session: " S1 " - " ?CID " - " ?NAME " - " ?WEIGHT " - " ?CF " ")
)

(defrule display_result2
 (declare (salience -702))
	(session_list (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID S2))
=>
	(printout t crlf "| Session: " S2 " - " ?CID " - " ?NAME " - " ?WEIGHT " - " ?CF " ")
)

(defrule display_result3
 (declare (salience -703))
	(session_list (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID S3))
=>
	(printout t crlf "| Session: " S3 " - " ?CID " - " ?NAME " - " ?WEIGHT " - " ?CF " ")
)

(defrule display_result4
 (declare (salience -704))
	(session_list (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID S4))
=>
	(printout t crlf "| Session: " S4 " - " ?CID " - " ?NAME " - " ?WEIGHT " - " ?CF " ")
)

(defrule display_result5
 (declare (salience -705))
	(session_list (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID S5))
=>
	(printout t crlf "| Session: " S5 " - " ?CID " - " ?NAME " - " ?WEIGHT " - " ?CF " ")
)

(defrule display_result6
 (declare (salience -706))
	(session_list (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID S6))
=>
	(printout t crlf "| Session: " S6 " - " ?CID " - " ?NAME " - " ?WEIGHT " - " ?CF " ")
)

(defrule display_result7
 (declare (salience -707))
	(session_list (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID S7))
=>
	(printout t crlf "| Session: " S7 " - " ?CID " - " ?NAME " - " ?WEIGHT " - " ?CF " ")
)

(defrule display_result8
 (declare (salience -708))
	(session_list (CID ?CID) (NAME ?NAME) (CF ?CF) (WEIGHT ?WEIGHT) (SID S8))
=>
	(printout t crlf "| Session: " S8 " - " ?CID " - " ?NAME " - " ?WEIGHT " - " ?CF " ")
	(printout t crlf "| ---------------------------------------------------------------------------" crlf)
)

